<template>
  <div class="announcement-detail">
    <div class="form-wrap">
      <el-form label-position="top" label-width="80px" :model="formData">
        <el-form-item :label="$t('configLazyLoad.process.nodeName')">
          <el-input v-model.trim="formData.DisplayName"></el-input>
        </el-form-item>

        <div class="edit-ivr-wrap" style="border: 1px solid #E7E7EB;margin-bottom: 20px;">
          <el-row style="padding: 6px 12px 4px 12px;background-color: #F4F5F9;">
            <el-col :span="3" v-if="isShowYear">年份</el-col>
            <el-col :span="3">日期</el-col>
            <el-col :span="4">时间</el-col>
            <el-col :span="5">星期</el-col>
            <el-col :span="5">转向</el-col>
            <el-col :span="3">
              <span style="display: inline-block;width: calc(100% - 20px)">优先级</span>
              <i @click="addIvrItem" class="el-icon-circle-plus" style="color: #409EFF;font-size: 18px;display: inline-block;width: 20px;cursor: pointer;"></i></el-col>
          </el-row>
          <div>
            <el-row v-for="(item, index) in formData.Dates" style="padding: 6px 6px 10px 6px;border-bottom: 1px solid #E7E7EB;">
              <el-col :span="3" v-if="isShowYear">
                <el-input style="width:calc(50% - 7px);" v-model="item.YearStartDate"></el-input><span style="display: inline-block;width: 14px;">一</span><el-input style="width:calc(50% - 7px);" v-model="item.YearEndDate"></el-input>
              </el-col>
              <el-col :span="3">
                <el-input style="width:calc(50% - 7px);" v-model="item.StartDate"></el-input><span style="display: inline-block;width: 14px;">一</span><el-input style="width:calc(50% - 7px);" v-model="item.EndDate"></el-input>
              </el-col>
              <el-col :span="4">
                <el-input style="width:calc(50% - 7px);" v-model="item.StartTime"></el-input><span style="display: inline-block;width: 14px;">一</span><el-input style="width:calc(50% - 7px);" v-model="item.EndTime"></el-input>
              </el-col>
              <el-col :span="5">
                <el-checkbox v-model="item.WeekMon">一</el-checkbox>
                <el-checkbox v-model="item.WeekTue">二</el-checkbox>
                <el-checkbox v-model="item.WeekWed">三</el-checkbox>
                <el-checkbox v-model="item.WeekThu">四</el-checkbox>
                <el-checkbox v-model="item.WeekFri">五</el-checkbox>
                <el-checkbox v-model="item.WeekSat">六</el-checkbox>
                <el-checkbox v-model="item.WeekSun">日</el-checkbox>
              </el-col>
              <el-col :span="5">
                <node-select-item :ref="'nodeSelect' + index" :curPbx="curPbx" :ToPeerType="item.NextPeerType" :ToPeer="item.NextPeer"></node-select-item>
              </el-col>
              <el-col :span="3">
                <el-input style="display: inline-block;width: calc(100% - 28px);margin-right: 5px;" type="number" v-model="item.Order"></el-input>
                <i @click="delIvrItem(index)" class="el-icon-remove" style="color: #FF6B6B;font-size: 18px;margin-top: 4px;display: inline-block;width: 20px;cursor: pointer;"></i>
              </el-col>
            </el-row>
          </div>
        </div>
        <div class="tip">
          <h6>温馨提示：</h6>
          <p>年份格式：年或*，如2020</p>
          <p>日期格式：月-日 或 *,如1-1</p>
          <p>时间格式：时:分 或 *,如09:00</p>
          <p>结束时间：结束时间的分钟设置为奇数,如18:59,即19点之前</p>
          <p>优先级：小于999的正整数,代表日程被匹配的次序,数字越小优先级越高,优先级越高的日程优先被匹配</p>
        </div>
        <el-form-item label="不满足条件转向：">
          <div style="width: 60%;">
            <node-select-item ref="nodeSelectUnmatched" :curPbx="curPbx" :ToPeerType="form.NextPeerUnmatchType" :ToPeer="form.NextPeerUnmatch"></node-select-item>
          </div>
        </el-form-item>

      </el-form>
    </div>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import {deepClone, isRepeat} from '@/utils/m7Utils'
  import nodeSelectItem from '../../base/nodeSelectItem.vue'
  export default {
    name: 'mainIvrDateNodeLink',
    data () {
      return {
        formData: {},
        isShowYear: false
      }
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {}
        }
      }
    },
    components: {
      nodeSelectItem
    },
    computed: {
      Account () {
        return this.$store.state.session.account.account
      },
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      }
    },
    methods: {
      init () {
        this.formData = deepClone(this.form)
        this.getIsShowYear(this.curPbx)
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      saveClick () {
        if (!this.formData.DisplayName) {
          this.$message({
            type: 'error',
            message: '节点名称不能为空!'
          })
          return
        }
        let datePattern = new RegExp(/^([1-9]|[1][012])-([1-9]|[12][0-9]|[3][01])$/)
        let timePattern = new RegExp(/^([01][0-9]|[2][0-3]):[0-5][0-9]$/)
        let yearPattern = new RegExp(/^\d{4}$/)
        let flag1 = false  //  日期、时间、优先级不能为空
        let flag2 = false   //  星期至少选一个
        let flag3 = false   //  优先级不能重复
        let flag4 = false   //  优先级  1-999
        let flag5 = false   //  请输入正确的开始日期
        let flag6 = false   //  请输入正确的结束日期
        let flag7 = false   //  请输入正确的开始时间
        let flag8 = false   //  请输入正确的结束时间
        let flag9 = false   //  设置日程结束时间，请将分钟设置为奇数，如18:59，即19点之前
        let flag10 = false // 开始年份
        let flag11 = false // 结束年份
        let OrderArr = []
        this.formData.Dates.forEach(item => {
          OrderArr.push(item.Order)
          if (!item.StartDate || !item.EndDate || !item.StartTime || !item.EndTime || !item.Order) {
            flag1 = true
          }
          if (!(item.WeekMon || item.WeekTue || item.WeekWed || item.WeekThu || item.WeekFri || item.WeekSat || item.WeekSun)) {
            flag2 = true
          }
          if (Number(item.Order) < 1 || Number(item.Order) > 999) {
            flag4 = true
          }
          if (!datePattern.test(item.StartDate) && item.StartDate !== '*') {
            flag5 = true
          }
          if (!yearPattern.test(item.YearStartDate) && item.YearStartDate !== '*') {
            flag10 = true
          }
          if (!yearPattern.test(item.YearEndDate) && item.YearEndDate !== '*') {
            flag11 = true
          }
          if (!datePattern.test(item.EndDate) && item.EndDate !== '*') {
            flag6 = true
          }
          if (!timePattern.test(item.StartTime) && item.StartTime !== '*') {
            flag7 = true
          }
          if (!timePattern.test(item.EndTime) && item.EndTime !== '*') {
            flag8 = true
          }
          if (item.EndTime !== '*') {
            let lastNum = item.EndTime.substring(item.EndTime.length - 1)
            if (lastNum % 2 === 0) {
              flag9 = true
            }
          }
        })
        if (flag1) {
          this.$message({
            type: 'error',
            message: '日期、时间、优先级不能为空'
          })
          return
        }
        if (flag2) {
          this.$message({
            type: 'error',
            message: '星期至少选一个'
          })
          return
        }
        flag3 = isRepeat(OrderArr)
        if (flag3) {
          this.$message({
            type: 'error',
            message: '优先级不能重复'
          })
          return
        }
        if (flag4) {
          this.$message({
            type: 'error',
            message: '优先级必须填写数字1-999之间'
          })
          return
        }
        if (flag5) {
          this.$message({
            type: 'error',
            message: '请输入正确的开始日期'
          })
          return
        }
        if (flag6) {
          this.$message({
            type: 'error',
            message: '请输入正确的结束日期'
          })
          return
        }
        if (flag7) {
          this.$message({
            type: 'error',
            message: '请输入正确的开始时间'
          })
          return
        }
        if (flag8) {
          this.$message({
            type: 'error',
            message: '请输入正确的结束时间'
          })
          return
        }
        if (flag9) {
          this.$message({
            type: 'error',
            message: '设置日程结束时间，请将分钟设置为奇数，如18:59，即19点之前'
          })
          return
        }
        if (flag10) {
          this.$message({
            type: 'error',
            message: '请输入正确的开始年份'
          })
          return
        }
        if (flag11) {
          this.$message({
            type: 'error',
            message: '请输入正确的结束年份'
          })
          return
        }
        this.formData.Dates.forEach((item, index) => {
          this.$set(item, 'NextPeerType', this.$refs['nodeSelect' + index][0].ToPeerTypeThis)
          this.$set(item, 'NextPeer', this.$refs['nodeSelect' + index][0].ToPeerThis)
        })
        this.formData.NextPeerUnmatchType = this.$refs.nodeSelectUnmatched.ToPeerTypeThis
        this.formData.NextPeerUnmatch = this.$refs.nodeSelectUnmatched.ToPeerThis
        let obj = deepClone(this.formData)
        obj.PBX = this.curPbx
        obj.Account = this.Account
        this.$store.dispatch('updateDate', obj).then(res => {
          if (res && res.success) {
            this.$message({
              type: 'success',
              message: '保存成功!'
            })
            this.updateVuexPbx()
          }
        })
      },
      updateVuexPbx () {
        this.$store.dispatch('queryDate', {Account: this.Account, PBX: this.curPbx}).then(resp => {
          if (resp && resp.success) {
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_date', data: resp.list || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      },
      addIvrItem () {
        let obj = {
          WeekMon: true,
          WeekTue: true,
          WeekWed: true,
          WeekThu: true,
          WeekFri: true,
          WeekSat: true,
          WeekSun: true,
          YearStartDate: '*',
          YearEndDate: '*',
          StartDate: '*',
          EndDate: '*',
          StartTime: '*',
          EndTime: '*',
          Order: 1,
          NextPeerType: '',
          NextPeer: ''
        }
        if (this.formData.Dates) {
          this.formData.Dates.push(obj)
        } else {
          let arr = []
          arr.push(obj)
          this.$set(this.formData, 'Dates', arr)
        }
      },
      delIvrItem (index) {
        this.formData.Dates.splice(index, 1)
      },
      getIsShowYear (pbx) {
        this.$store.dispatch('getNewFeaturesReport', {pbx: pbx}).then((res)=>{
          if (res.data.features && res.data.features.indexOf('YearAdd') > -1) {
            this.isShowYear = true
          } else {
            this.isShowYear = false
          }
        })
      }
    },
    watch: {
      form: {
        deep: true,
        handler (nv, ov) {
          this.init()
          this.$refs.nodeSelectUnmatched.init()
        }
      }
    },
    beforeMount () {
      this.init()
    }
  }
</script>

<style lang="stylus" scoped>
  .announcement-detail
    position relative
    width 100%
    height 100%
  .form-wrap
    padding 20px
    height calc(100% - 100px)
    overflow-y scroll
    .el-col
      padding-right 4px
    .el-checkbox
      margin-left 10px
    .el-checkbox__label
      padding-left 4px
  .edit-ivr-wrap
    /*max-height calc(100vh - 350px)*/
    /*overflow-y scroll*/
    background-color #fff
  .tip
    padding-bottom 20px
    color #3B5998
    font-size 12px
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
